<?php
namespace app\admin\controller;
use \app\admin\model\Member;
use \app\admin\model\YjsVerify;
use \app\admin\model\User;
use app\admin\model\AuthAssignment;
use app\admin\controller\BaseController;
use app\admin\model\UserAli;
use app\admin\model\UserBank;
use app\admin\model\AuthMember;
use app\admin\model\MemberMenus;
use \think\Db;
class Login extends BaseController {

	public function index()
    {
		return view('index', compact(''));
	}
	public function login(Member $Member, AuthMember $auth_member)
    {
//		if (!captcha_check(input('post.verify'))) {
//            $this->error('验证码不正确', 'login/index');
//        }
		$list = $Member->where('user_name', '=', input('post.username'))->find();
        if($list) {
            if($list['password'] == md5(input('post.passwd'))) {
            	$auth_members = $auth_member->where(['member_id' => $list['id']])->field('auth_name')->select();
            	if(!$auth_members){
            		$this->error('暂无登陆权限');
            	}
				session('member_id', $list['id']);
				session('member_name', $list['user_name']);
				session('auth_name', $auth_members[0]['auth_name']);
				$last_time = $Member::get($list['id']);
                $last_time->login_time = time();
                $last_time->ip = $_SERVER['REMOTE_ADDR'];
                $last_time->save();
				$this->success('登录成功','index/index');
			} else {
				$this->error('密码不正确');
			}
		} else {
			$this->error('用户名不存在', 'login/index');
		}
	}
	public function logout(Member $Member)
    {
//		$last_time = $Member::get(session('member_id'));
		$last_time['logout_time'] = time();
//		$last_time->save();
		$logout = $Member->where('id',session('member_id'))->update($last_time);
		if(!$logout) $this->error('操作失败，请联系管理员');
		Session(null);
		$this->redirect('login/index');
	}
	/**
	 * 渲染匀加速注册界面
	 * @return [type] [description]
	 */
	public function yjxregister()
	{
		return view('', compact(''));
	}
	/**
	 * 匀加速方用户本平台注册
	 * @param  User   $user [description]
	 * @return [type]       [description]
	 */
	public function checkYjsregister(User $user)
	{
		if(!input('post.username')  || !input('post.beans') || !input('post.username') || !input('post.user_name') || !input('post.bank_num') ){
			return $this->error('参数不能为空');
		}
		$post['username'] = input('post.username');
		$post['password'] = md5(123456);
		$post['iphone'] = input('post.username');
		$post['beans'] = input('post.beans');
		$post['user_name'] = input('post.user_name');
		if(!preg_match("/^\d*$/",input('post.beans'))) {
			return $this->error('联豆数量必须为数值');
		}
		if(!preg_match("/^\d*$/",input('post.bank_phone'))) {
			return $this->error('银行预留手机号必须为数值');
		}
		if(!preg_match("/^\d*$/",input('post.bank_num'))) {
			return $this->error('银行卡号必须为数值');
		}
		$post['create_time'] = time();
		$post['is_use'] = 0;
		$post['user_where'] = 2;
		$post['ip'] = $_SERVER['REMOTE_ADDR'];
		if(strlen($post['username']) != '11') {
			return $this->error('请填写正确的手机号位数');
		}
		if(strlen(input('post.bank_phone')) != '11') {
			return $this->error('请填写正确的银行卡手机号位数');
		}
		$search ='/^(1(([34578][0-9])|(47)|[8][0126789]))\d{8}$/';
		if(!preg_match($search,$post['username'])) {
 			return $this->error('请填写正确的手机号码');
		}
		if(!preg_match($search,input('post.bank_phone'))) {
 			return $this->error('请填写正确的银行卡手机号');
		}
		if(strlen(input('post.bank_num')) > 19 || strlen(input('post.bank_num')) < 16) {
			return $this->error('请填写正确的银行卡号');
		}
		$verify = new YjsVerify();
		$verify_query = $verify->where(['verify' => input('post.verify')])->select()->toArray();
		if(!$verify_query) {
			return $this->error('校验码不正确');
		}
		
		$users = $user->where(['username' => $post['username']])->select()->toArray();
		if($users) {
			return $this->error('用户已存在');
		}
		
		Db::startTrans();
		try {
			$result = $user->save($post);
			if(!$result) {
				throw new \Exception('录入失败');
			}
			$auth_assignment = new AuthAssignment();
			$datas['user_id'] = $user->id;
			$datas['item_name'] = 'staff';
			$datas['created_at'] = time();
			$auth = $auth_assignment->save($datas);
			if(!$auth) {
				throw new \Exception('录入失败');
			}
			$bank['bank_num'] = input('post.bank_num');
			$bank['name'] = input('post.user_name');
			$bank['iphone'] = input('post.bank_phone');
			$bank['bank_addr'] = input('post.bank_addr');
			$bank['bank_name'] = input('post.bank_name');
			$bank['bank_staff'] = input('post.bank_staff');
			$bank['create_time'] = time();
			$bank['user_id'] = $user->id;
			$user_bank = new UserBank();
			$user_banks = $user_bank->save($bank);
			if(!$user_banks) {
				throw new \Exception('录入失败');
			}
			$ali = new UserAli();
			$alis['user_id'] = $user->id;
			$alis['staff'] = 1;
			$alis['num'] = input('post.ali_num');
			$alis['user_name'] = input('post.user_name');
			$alis['create_time'] = time();
			$ali_result = $ali->save($alis);
			if(!$ali_result) {
				throw new \Exception('录入失败');
			}
			Db::commit(); 
		} catch (\Exception $e) {
			Db::rollback();
			return $this->error($e->getmessage());
		}
		return $this->success('录入成功');
	}
}